
var numAi = 0
var timer
Page({
  data:{
    //控制按钮是否可点击
    btnState:false,
    //记录获胜次数
    winNum:0,
    //中间的话“Ho~ You Win”
    gameOfPlay:'',
    //用户选择的图片
    imageUserScr:'/images/chuquan/wenhao.png',
    avatarUrl: '/images/chuquan/wenhao.png',
    //电脑随机的图片
    imageAiScr:'/images/chuquan/robot.jpg',
    //石头剪刀布图片数组
    srcs:[
      '/images/chuquan/shitou.png',
      '/images/chuquan/jiandao.png',
      '/images/chuquan/bu.png'
    ],
    face:null,
    gamePass:false
  },

  //生命周期，刚进来
  onLoad: function () {
    //获取本地缓存“已经获胜的次数”
    //wx.getStorageSync('winNum');
    var oldWinNum = null;
    //如果有缓存，那么赋值，否则为0
    if(oldWinNum != null && oldWinNum !=''){
       this.data.winNum = oldWinNum;
    }
    this.timerGo();
    console.info("==========");
    var that = this;
    wx.login({
      success: function () {
        wx.getUserInfo({
          success: function (res) {
            console.info("=====success=====");
            console.info(res);
            that.setData({ avatarUrl: res.userInfo.avatarUrl });
            that.setData({ imageUserScr: that.data.avatarUrl});
          }
        })
      }
    })

    // 获取用户信息
    wx.getSetting({
      success: res => {
        console.info("--------");
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              //this.globalData.userInfo = res.userInfo
              console.info("==res.userInfo.avatarUrl==");
              console.info(res.userInfo.avatarUrl);

              that.setData({imageUserScr: res.userInfo.avatarUrl});
            }
          })
        }
      }
    });
  },

  //点击按钮
  changeForChoose(e){
    console.log();
      if(this.data.btnState == true){
        return;
      }

      //如果是机器人图片，这个时候的出拳不算数
      if (this.data.imageAiScr == "/images/chuquan/robot.jpg") {
        return;
      }

      //获取数组中用户的，石头剪刀布相应的图片。
      this.setData({
          imageUserScr:this.data.srcs[e.currentTarget.id]
      });
      //清除计时器
      clearInterval(timer);

      //获取数据源
      var user = this.data.imageUserScr;
      var ai = this.data.imageAiScr;
      var num = this.data.winNum;
      var str = '0.0~\nYou Lost!';
      var face=null;
      //判断是否获胜
      if (user == "/images/chuquan/shitou.png" && ai == "/images/chuquan/jiandao.png"){
         //获胜后增加次数、改变文字内容、从新缓存获胜次数
         num++;
         str = 'Ho~\nYou Win!';
         face ="/images/chuquan/face-smile.jpg";
         wx.setStorageSync('winNum', num);
      }else if(user == "/images/chuquan/jiandao.png" && ai == "/images/chuquan/bu.png"){
         num++;
         str = 'Ho~\nYou Win!';
         face = "/images/chuquan/face-smile.jpg";
         wx.setStorageSync('winNum', num);
      }else if(user== "/images/chuquan/bu.png" && ai == "/images/chuquan/shitou.png"){
         num++;
         str = 'Ho~\nYou Win!';
         face = "/images/chuquan/face-smile.jpg";
         wx.setStorageSync('winNum', num);
      } else if (user == ai) {
        num = 0;
        str = 'Game Draw!';
        face = "/images/chuquan/face-ping.jpg";
      }else{
        face = "/images/chuquan/face-lost.jpg";
        num = 0;
      }

      if (num<3)
      {
        //刷新数据
        this.setData({
          winNum: num,
          gameOfPlay: str,
          btnState: true,
          face: face
        });
        setTimeout(this.again, 1000); 
      }else{
        //挑战成功
        this.setData({
          winNum: 3,
          face: null,
          gamePass:true
        });

        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/result/result'
          })
        }, 2000); 
      }
  },

  //开启计时器
  timerGo(){
    timer = setInterval(this.move,1000);
  },

  //ai滚动方法
  move(){
    //如果大于等于3，重置
    if(numAi>=3){
      numAi=0;
    }
    this.setData({
        //获取数组中Ai的，石头剪刀布相应的图片。
        imageAiScr: this.data.srcs[numAi],
    })
    numAi++;
  },

  again(){
      //控制按钮
      if(this.data.btnState == false){
        return;
      }
      //从新开始计时器
      this.timerGo();
      //刷新数据
      this.setData({
          btnState:false,
          gameOfPlay:'',
          imageUserScr: this.data.avatarUrl,
          imageAiScr: '/images/chuquan/robot.jpg',
          face: null
      });
  }
})